package com.haisen.dataobject.dto;

import com.alibaba.druid.sql.visitor.functions.Isnull;
import com.alibaba.fastjson.JSON;
import com.haisen.dataobject.entity.ItemCat;
import com.haisen.dataobject.entity.ItemParamGroup;
import com.haisen.dataobject.entity.Log;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
public class ItemCatDto {

    private Long id;
    private Long parentId;
    private String parentIds;
    private String name;
    private String paramGroupIds;
    private List<Long> groupIds;
    private List<ItemParamGroup> parmGroupIdList;
    private Integer up;
    private Integer sortOrder;
    private Boolean leaf;
    private String groupnames;
    private String groupJson;



    // private List<Long> parmGroupIdList;

    public void setGroupJson(String groupJson) {
      List<ItemParamGroup> itemParamGroupList = JSON.parseArray(groupJson,ItemParamGroup.class);
      StringBuilder s = new StringBuilder();
      itemParamGroupList.forEach( o ->{
          s.append(o.getId()+",");
      });
      if (s.length() >0 ){
          this.paramGroupIds = s.deleteCharAt(s.length() - 1).toString();
      }
        this.groupJson = groupJson;
    }

    public void setParmGroupIdList(List<ItemParamGroup> parmGroupIdList) {
        StringBuilder s = new StringBuilder();
        parmGroupIdList.forEach(o ->{
            s.append(o.getGroupName()+",");
        });
        if (s.length() > 0){
            this.groupnames = s.deleteCharAt(s.length()-1).toString();
        }
        this.parmGroupIdList = parmGroupIdList;
    }

    public List<Long> getGroupIds() {
        if(groupIds == null){
            groupIds = new ArrayList<>();
        }
        return groupIds;
    }

    public void setparamGroupIds(String paramGroupIds) {
        //1根据,解析为数组
        String[] groupArray = paramGroupIds.split(",");
        //2添加到list中
        Arrays.asList(groupArray).forEach(o ->{
            if (!StringUtils.isEmpty(o)) {

                getGroupIds().add(Long.valueOf(o));
            }
        });
        this.paramGroupIds = paramGroupIds;
    }


    public ItemCatDto() {
    }

/*  public ItemCatDto(Long id, Long parentId, String name, Integer up, Integer sortOrder, Boolean leaf) {
        this.id = id;
        this.parentId = parentId;
        this.parentIds = parentIds;
        this.name = name;
        this.up = up;
        this.sortOrder = sortOrder;
        this.leaf = leaf;
    }*/

    public ItemCatDto(ItemCat itemCat) {
        this.id = itemCat.getId();
        this.parentId = itemCat.getParentId();
        this.parentIds = itemCat.getParentIds();
        this.name = itemCat.getName();
        this.paramGroupIds = itemCat.getParamGroupIds();
        this.parmGroupIdList = itemCat.getParmGroupIdList();
        this.up = itemCat.getUp();
        this.sortOrder = itemCat.getSortOrder();
        this.leaf = itemCat.getLeaf();
    }
}
